package MyApp.View
{
	import MyApp.Context;
	import MyApp.Controller.ReportCommand;
	import MyApp.Controller.WellCommand;
	import MyApp.Controller.WellGroupCommand;
	import MyApp.Model.Authority;
	import MyApp.Model.AuthorityData;
	import MyApp.Model.LoginUser;
	import MyApp.Model.User;
	import MyApp.Model.proxy.ReportProxy;
	import MyApp.Model.proxy.WellGroupProxy;
	import MyApp.Model.proxy.WellProxy;
	import MyApp.View.report.ReportMediator;
	import MyApp.View.well.ObservationRecordMediator;
	import MyApp.View.well.ProductionRecordMediator;
	import MyApp.View.well.WellMediator;
	import MyApp.View.wellGroup.WellGroupMediator;
	
	import components.MainDesk;
	import components.report.ReportWindow;
	import components.well.ObservationRecord;
	import components.well.ProductionRecord;
	import components.well.WellWindow;
	import components.wellGroup.WellGroupWindow;
	
	import custom.pager.Page;
	import custom.pager.Pager;
	
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.core.FlexGlobals;
	import mx.events.CloseEvent;
	import mx.rpc.AbstractOperation;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	import net.lanelife.framework.cw.Window;
	import net.lanelife.framework.cw.WindowManager;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import util.Constants;
	import util.Event.ComponentCreatedEvent;
	import util.Event.ValueEvent;
	import util.GDUtil;
	
	import windows.BaseWindow;
	
	public class MainDeskMediator extends Mediator
	{
		public static const NAME:String="MainDeskMediator";
		public function MainDeskMediator(mediatorName:String=null, viewComponent:Object=null)
		{
			super(mediatorName, viewComponent);
			mainView.addEventListener(MainDesk.SELECT_ITEM,selectItem);
			mainView.addEventListener(MainDesk.NULLIFY_USER,nullifyUser);
			//创建完成事件
			mainView.addEventListener(MainDesk.ON_WELL_GROUP_CREATED,onWellGroupCreated);
			mainView.addEventListener(MainDesk.ON_WELL_CREATED,onWellCreated);
			mainView.addEventListener(MainDesk.ON_REPORT_CREATED,onReportCreated);
		}
		private function get mainView():MainDesk{
			return viewComponent as MainDesk;
		}
		private function openBaseWindow(windowClass:Class,id:String):Window{
			var baseWindow:Window = WindowManager.get(id) as Window;
			if (!baseWindow) 
			{
				baseWindow = new windowClass();
				baseWindow.id = id;
			}
			baseWindow.show();
			baseWindow.x=FlexGlobals.topLevelApplication.width/2-baseWindow.width/2;//水平居中
			return baseWindow;
		}
		protected function onReportCreated(e:ComponentCreatedEvent):void{
			facade.registerMediator(new ReportMediator(ReportMediator.NAME,e.component));
			facade.registerCommand(ReportWindow.DO_REFRESH_GROUP,ReportCommand);
			facade.registerCommand(ReportWindow.DO_REFRESH_REPORT1,ReportCommand);
			facade.registerCommand(ReportWindow.DO_REFRESH_REPORT2,ReportCommand);
			facade.registerProxy(new ReportProxy(ReportProxy.NAME));
			this.sendNotification(ReportWindow.DO_REFRESH_GROUP);
		}
		protected function onWellCreated(e:ComponentCreatedEvent):void{
			facade.registerMediator(new WellMediator(WellMediator.NAME,e.component));
			facade.registerMediator(new ObservationRecordMediator(ObservationRecordMediator.NAME,e.component.obserRecord));
			facade.registerCommand(ObservationRecord.DO_REFRESH,WellCommand);
			facade.registerCommand(WellWindow.DO_REFRESH,WellCommand);
			facade.registerCommand(WellWindow.DO_SAVE,WellCommand);
			facade.registerCommand(WellWindow.DO_ADD_OBSRECORD,WellCommand);
			facade.registerCommand(WellWindow.DO_UPDATE_OBSRECORD,WellCommand);
			facade.registerCommand(WellWindow.DO_DELETE_OBSRECORD,WellCommand);
			facade.registerCommand(WellWindow.DO_ADD_PRDRECORD,WellCommand);
			facade.registerCommand(WellWindow.DO_UPDATE_PRDRECORD,WellCommand);
			facade.registerCommand(WellWindow.DO_DELETE_PRDRECORD,WellCommand);
			facade.registerCommand(ProductionRecord.DO_REFRESH,WellCommand);
			facade.registerProxy(new WellProxy(WellProxy.NAME));
			this.sendNotification(WellWindow.DO_REFRESH);
		}
		protected function onWellGroupCreated(e:ComponentCreatedEvent):void{
			facade.registerMediator(new WellGroupMediator(WellGroupMediator.NAME,e.component));
			facade.registerCommand(WellGroupWindow.DO_REFRESH,WellGroupCommand);
			facade.registerCommand(WellGroupWindow.DO_ADD_WELL_GROUP,WellGroupCommand);
			facade.registerCommand(WellGroupWindow.DO_ADD_WELL,WellGroupCommand);
			facade.registerCommand(WellGroupWindow.DO_REFRESH_WELL,WellGroupCommand);
			facade.registerCommand(WellGroupWindow.DO_SAVE_WELL_GROUP,WellGroupCommand);
			facade.registerCommand(WellGroupWindow.DO_UNBIND,WellGroupCommand);
			facade.registerCommand(WellGroupWindow.DO_BIND,WellGroupCommand);
			facade.registerCommand(WellGroupWindow.DO_DELETE,WellGroupCommand);
			facade.registerCommand(WellGroupWindow.DO_DELETE_WELL,WellGroupCommand);
			facade.registerProxy(new WellGroupProxy(WellGroupProxy.NAME));
			this.sendNotification(WellGroupWindow.DO_REFRESH,new Pager());
		}
		public function nullifyUser(e:MouseEvent):void {
			Alert.show("注销用户，将会重新登录","提示",Alert.YES|Alert.NO,null,turnLoginView);
		}
		
		
		
		public function turnLoginView(e:CloseEvent):void {
			if(e.detail == Alert.YES) {
				/*FlexGlobals.topLevelApplication.main.mainVs.selectedChild=*/
				FlexGlobals.topLevelApplication.vs.selectedIndex = 0;
			}
		}
		private function selectItem(e:ValueEvent):void{
			switch(e.vo){
				case "矿井管理":
//					laodSpecificAuthorities(Constants.AUTHORITY_1);
					openBaseWindow(WellGroupWindow,WellGroupWindow.NAME);
//					this.sendNotification(WellGroupWindow.DO_REFRESH,new Pager());
					break;
				case "数据管理":
					openBaseWindow(WellWindow,WellWindow.NAME);
//					this.sendNotification(WellWindow.DO_REFRESH);
					break;
				case "月度报表":
					openBaseWindow(ReportWindow,ReportWindow.NAME);
					break;
			}
		} 
		//查询每一块具体的的权限
		public function laodSpecificAuthorities(parentCode:String):void {
			var remote:RemoteObject =  GDUtil.remoteObject();
			remote.destination = Constants.AUTHORITY_SER;
			var aoAuthority:AbstractOperation = remote.getOperation("laodSpecificAuthorities");;
			aoAuthority.addEventListener(ResultEvent.RESULT,function(e:ResultEvent):void{
				filterAuthority(e.result as ArrayCollection);
			
			});
			aoAuthority.addEventListener(FaultEvent.FAULT,function (e:FaultEvent):void {
				Alert.show("加载数据失败!");			
			});
			//Context.instance.setProperty(Constants.LOGINUSER_KEY,loginuser);
			var loginuser:LoginUser = LoginUser(Context.instance.getProperty(Constants.LOGINUSER_KEY));
			var user:User = loginuser.user as User;
			aoAuthority.send(user.roleid,parentCode);
		}

		//赋值于变量
		private function filterAuthority(setedAuthirity:ArrayCollection):void
		{
			if(setedAuthirity!=null && setedAuthirity.length>0) {
				
				for(var i:int = 0;i<setedAuthirity.length;i++) {
					var au:Authority = setedAuthirity.getItemAt(i) as Authority;
					switch(au.authorityCode) {
						case Constants.AUTHORITY_18:
							AuthorityData.agent_authority_1 = true;  //新增代理商
							break;
						case Constants.AUTHORITY_19:
							AuthorityData.agent_authority_2 = true;  //修改代理商
							break;
						case Constants.AUTHORITY_20:
							AuthorityData.agent_authority_3 = true;  //删除代理商
							break;
						case Constants.AUTHORITY_21:
							AuthorityData.agent_authority_4 = true;  //查看代理商
							break;
						case Constants.AUTHORITY_22:
							AuthorityData.store_authority_1 = true;  //新增店
							break;
						case Constants.AUTHORITY_23:
							AuthorityData.store_authority_2 = true;  //修改店
							break;
						case Constants.AUTHORITY_24:
							AuthorityData.store_authority_3 = true;  //删除店
							break;
						case Constants.AUTHORITY_25:
							AuthorityData.store_authority_4 = true;  //停用/启用店
							break;
						case Constants.AUTHORITY_26:
							AuthorityData.store_authority_5 = true;  //查看店
							break;
						case Constants.AUTHORITY_27:
							AuthorityData.store_authority_6 = true;  //查询店
							break;
						case Constants.AUTHORITY_28:
							AuthorityData.store_authority_7 = true;  //现金充值
							break;
						case Constants.AUTHORITY_29:
							AuthorityData.store_authority_8 = true;  //充值卡充值
							break;
						case Constants.AUTHORITY_30:
							AuthorityData.store_authority_9 = true;  //赠送金额
							break;
						case Constants.AUTHORITY_31:
							AuthorityData.store_authority_10 = true;  //提现
							break;
						case Constants.AUTHORITY_32:
							AuthorityData.store_authority_11 = true;  //查看账号明细
							break;
						case Constants.AUTHORITY_33:
							AuthorityData.store_authority_12 = true;  //消费
							break;
						case Constants.AUTHORITY_34:
							AuthorityData.store_authority_13 = true;  //查看消费明细
							break;
						case Constants.AUTHORITY_35:
							AuthorityData.store_authority_14 = true;  //修改店到期时间
							break;
						case Constants.AUTHORITY_36:
							AuthorityData.store_authority_15 = true;  //修改短信条数
							break;
						case Constants.AUTHORITY_37:
							AuthorityData.store_authority_16 = true;  //开启/关闭数据同步
							break;
						case Constants.AUTHORITY_38:
							AuthorityData.charged_card_authority_1 = true;  //新增充值卡
							break;
						case Constants.AUTHORITY_39:
							AuthorityData.charged_card_authority_2 = true;  //修改充值卡
							break;
						case Constants.AUTHORITY_40:
							AuthorityData.charged_card_authority_3 = true;  //删除充值卡
							break;
						case Constants.AUTHORITY_41:
							AuthorityData.charged_card_authority_4 = true;  //查看充值卡
							break;
						case Constants.AUTHORITY_42:
							AuthorityData.charged_card_authority_5 = true;  //下发充值卡
							break;
						case Constants.AUTHORITY_43:
							AuthorityData.charged_card_authority_6 = true;  //查询充值卡
							break;
						case Constants.AUTHORITY_44:
							AuthorityData.voucher_authority_1 = true;  //新增代金卷
							break;
						case Constants.AUTHORITY_45:
							AuthorityData.voucher_authority_2 = true;  //修改代金卷
							break;
						case Constants.AUTHORITY_46:
							AuthorityData.voucher_authority_3 = true;  //删除代金卷
							break;
						case Constants.AUTHORITY_47:
							AuthorityData.voucher_authority_4 = true;  //查看代金卷
							break;
						case Constants.AUTHORITY_48:
							AuthorityData.voucher_authority_5 = true;  //下发代金卷
							break;
						case Constants.AUTHORITY_49:
							AuthorityData.voucher_authority_6 = true;  //查询代金卷
							break;
						case Constants.AUTHORITY_50:
							AuthorityData.store_sms_authority_1 = true;  //查看店家短信记录
							break;
						case Constants.AUTHORITY_51:
							AuthorityData.store_sms_authority_2 = true;  //查询点店家短信记录
							break;
						case Constants.AUTHORITY_52:
							AuthorityData.product_authority_1 = true;  //新增产品
							break;
						case Constants.AUTHORITY_53:
							AuthorityData.product_authority_2 = true;  //修改产品
							break;
						case Constants.AUTHORITY_54:
							AuthorityData.product_authority_3 = true;  //删除产品
							break;
						case Constants.AUTHORITY_55:
							AuthorityData.product_authority_4 = true;  //查看产品
							break;
						case Constants.AUTHORITY_56:
							AuthorityData.product_authority_5 = true;  //查询产品
							break;
						case Constants.AUTHORITY_57:
							AuthorityData.product_unit_authority_1 = true;  //新增产品单位
							break;
						case Constants.AUTHORITY_58:
							AuthorityData.product_unit_authority_2 = true;  //修改产品单位
							break;
						case Constants.AUTHORITY_59:
							AuthorityData.product_unit_authority_3 = true;  //删除产品单位
							break;
						case Constants.AUTHORITY_60:
							AuthorityData.product_unit_authority_4 = true;  //查看产品单位
							break;
						case Constants.AUTHORITY_61:
							AuthorityData.product_unit_authority_5 = true;  //查询产品单位
							break;
						case Constants.AUTHORITY_62:
							AuthorityData.order_center_authority_1 = true;  //选择产品
							break;
						case Constants.AUTHORITY_63:
							AuthorityData.order_center_authority_2 = true;  //查询购买记录
							break;
						case Constants.AUTHORITY_64:
							AuthorityData.order_center_authority_3 = true;  //分类查询产品
							break;
						case Constants.AUTHORITY_65:
							AuthorityData.order_center_authority_4 = true;  //购买产品
							break;
						case Constants.AUTHORITY_66:
							AuthorityData.sms_authority_1 = true;  //选择店家
							break;
						case Constants.AUTHORITY_67:
							AuthorityData.sms_authority_2 = true;  //选择代理商
							break;
						case Constants.AUTHORITY_68:
							AuthorityData.sms_authority_3 = true;  //手动添加手机号码
							break;
						case Constants.AUTHORITY_69:
							AuthorityData.sms_authority_4 = true;  //发送短信
							break;
						case Constants.AUTHORITY_70:
							AuthorityData.sms_center_authority_1 = true;  //新增注册号
							break;
						case Constants.AUTHORITY_71:
							AuthorityData.sms_center_authority_2 = true;  //激活/注销注册号
							break;
						case Constants.AUTHORITY_72:
							AuthorityData.sms_center_authority_3 = true;  //充值
							break;
						case Constants.AUTHORITY_73:
							AuthorityData.sms_center_authority_4 = true;  //手动添加注册记录
							break;
						case Constants.AUTHORITY_74:
							AuthorityData.sms_center_authority_5 = true;  //查看充值记录
							break;
						case Constants.AUTHORITY_75:
							AuthorityData.sms_record_authority_1 = true;  //查看短信记录
							break;
						case Constants.AUTHORITY_76:
							AuthorityData.sms_record_authority_2 = true;  //查询短信记录
							break;
						case Constants.AUTHORITY_77:
							AuthorityData.message_authority_1 = true;  //新增消息
							break;
						case Constants.AUTHORITY_78:
							AuthorityData.message_authority_2 = true;  //删除消息
							break;
						case Constants.AUTHORITY_79:
							AuthorityData.message_authority_3 = true;  //查看消息
							break;
						case Constants.AUTHORITY_80:
							AuthorityData.staff_authority_1 = true;  //新增员工
							break;
						case Constants.AUTHORITY_81:
							AuthorityData.staff_authority_2 = true;  //修改员工
							break;
						case Constants.AUTHORITY_82:
							AuthorityData.staff_authority_3 = true;  //删除员工
							break;
						case Constants.AUTHORITY_83:
							AuthorityData.staff_authority_4 = true;  //查看员工
							break;
						case Constants.AUTHORITY_84:
							AuthorityData.staff_authority_5 = true;  //修改员工密码
							break;
						case Constants.AUTHORITY_85:
							AuthorityData.staff_authority_6 = true;  //查询员工
							break;
						case Constants.AUTHORITY_86:
							AuthorityData.role_authority_1 = true;  //新增角色
							break;
						case Constants.AUTHORITY_87:
							AuthorityData.role_authority_2 = true;  //修改角色
							break;
						case Constants.AUTHORITY_88:
							AuthorityData.role_authority_3 = true;  //删除角色
							break;
						case Constants.AUTHORITY_89:
							AuthorityData.role_authority_4 = true;  //设置权限
							break;
						case Constants.AUTHORITY_90:
							AuthorityData.version_authority_1 = true;  //新增版本
							break;
						case Constants.AUTHORITY_91:
							AuthorityData.version_authority_2 = true;  //修改版本
							break;
						case Constants.AUTHORITY_92:
							AuthorityData.version_authority_3 = true;  //删除版本
							break;
						case Constants.AUTHORITY_93:
							AuthorityData.version_authority_4 = true;  //查看版本
							break;
						case Constants.AUTHORITY_94:
							AuthorityData.version_authority_5 = true;  //查询版本
							break;
						case Constants.AUTHORITY_95:
							AuthorityData.synchronous_authority_1 = true;  //新增非同步表
							break;
						case Constants.AUTHORITY_96:
							AuthorityData.synchronous_authority_2 = true;  //修改非同步表
							break;
						case Constants.AUTHORITY_97:
							AuthorityData.synchronous_authority_3 = true;  //删除非同步表
							break;
						case Constants.AUTHORITY_98:
							AuthorityData.synchronous_authority_4 = true;  //查询非同步表
							break;
						case Constants.AUTHORITY_99:
							AuthorityData.mktstore_authority_1 = true;  //查询非同步表
							break;
						case Constants.AUTHORITY_100:
							AuthorityData.mktstore_authority_2 = true;  //查询非同步表
							break;
						case Constants.AUTHORITY_101:
							AuthorityData.mktstore_authority_3 = true;  //查询非同步表
							break;
						case Constants.AUTHORITY_102:
							AuthorityData.mktstore_authority_4 = true;  //查询非同步表
							break;
						case Constants.AUTHORITY_103:
							AuthorityData.mktstore_authority_5 = true;  //查询非同步表
							break;
						case Constants.AUTHORITY_104:
							AuthorityData.mktstore_authority_6 = true;  //查询非同步表
							break;
						case Constants.AUTHORITY_105:
							AuthorityData.mktstore_authority_7 = true;  //查询非同步表
							break;
						case Constants.AUTHORITY_106:
							AuthorityData.mktstore_authority_8 = true;  //查询非同步表
							break;
						case Constants.AUTHORITY_107:
							AuthorityData.mktstore_authority_9 = true;  //查询非同步表
							break;
						case Constants.AUTHORITY_108:
							AuthorityData.mktstore_authority_10 = true;  //查询非同步表
							break;
						case Constants.AUTHORITY_109:
							AuthorityData.sms_center_authority_6=true;//新增统一充值记录
							break;
						case Constants.AUTHORITY_110:
							AuthorityData.sms_center_authority_7=true;//查看全部充值记录
							break;
						case Constants.AUTHORITY_111:
							AuthorityData.order_center_authority_5=true;//查看订单列表
							break;
						case Constants.AUTHORITY_112:
							AuthorityData.sms_record_authority_3=true;//重发短息记录
							break;
						case Constants.AUTHORITY_113:
							AuthorityData.sms_record_authority_4=true;//删除短息记录
							break;
						
					}
				}
				
			}
			
		}
		
		
		
		
		
		
		
		
		//加载桌面的window 通过权限判断
		public function loadDesktopView(authorityList:ArrayCollection):void {
			mainView.content.addElement(mainView.wellGroup);
			mainView.content.addElement(mainView.well);
			mainView.content.addElement(mainView.report);
//			if(authorityList != null && authorityList.length>0) {
//				for(var i:int =0;i<authorityList.length;i++) {
//					var au:Authority = authorityList.getItemAt(i) as Authority;
//					switch(au.authorityCode) {
//						case Constants.AUTHORITY_1://代理商管理
//							mainView.content.addElementAt(mainView.agent,0);
//							break;
//						case Constants.AUTHORITY_2://店家管理
//							mainView.content.addElement(mainView.store_manager);
//							break;
//						case Constants.AUTHORITY_3://充值卡管理
//							mainView.content.addElement(mainView.charged_card);
//							break;
//						case Constants.AUTHORITY_4://代金卷管理
//							mainView.content.addElement(mainView.voucher);
//							break;
//						case Constants.AUTHORITY_5://店家短信充值记录
//							mainView.content.addElement(mainView.chargeRecord);
//							break;
//						case Constants.AUTHORITY_6://产品管理
//							mainView.content.addElement(mainView.product_manager);
//							break;
//						case Constants.AUTHORITY_7://产品单元管理
//							mainView.content.addElement(mainView.product_unit);
//							break;
//						case Constants.AUTHORITY_8://订购中心
//							mainView.content.addElement(mainView.ordercenter);
//							break;
//						case Constants.AUTHORITY_9://发短信
//							mainView.content.addElement(mainView.sms_send);
//							break;
//						case Constants.AUTHORITY_10://短信平台
//							mainView.content.addElement(mainView.sms_center);
//							break;
//						case Constants.AUTHORITY_11://短信记录
//							mainView.content.addElement(mainView.sms_record);
//							break;
//						case Constants.AUTHORITY_12://发布消息
//							mainView.content.addElement(mainView.message_center);
//							break;
//						case Constants.AUTHORITY_13://员工管理
//							mainView.content.addElement(mainView.staff);
//							break;
//						case Constants.AUTHORITY_14://权限管理
//							mainView.content.addElement(mainView.authority);
//							break;
//						case Constants.AUTHORITY_15://版本管理
//							mainView.content.addElement(mainView.database_version);
//							break;
//						case Constants.AUTHORITY_16:///数据库同步管理
//							mainView.content.addElement(mainView.synchro_manager);
//							break;
//						case Constants.AUTHORITY_17:///潜在客官管理
//							mainView.content.addElement(mainView.marketStoreView);
//							break;
//						case Constants.AUTHORITY_114://发型图库管理
//							mainView.content.addElement(mainView.hairView);
//							break;
//							
//					}
//				}
//			}
				
			
		}
		
		
		override public function handleNotification(notification:INotification):void
		{
			// TODO Auto Generated method stub
			switch(notification.getName()) {
				case LoginMediator.INIT_PORTAL_VIEW_OK:
					var authorityList:ArrayCollection = notification.getBody() as ArrayCollection;
					loadDesktopView(authorityList);
					break;
				
			}
		}
		override public function listNotificationInterests():Array
		{
			// TODO Auto Generated method stub
			return [LoginMediator.INIT_PORTAL_VIEW_OK];
		}
		
		
	}
}